import { _decorator, Component, instantiate, Node, Prefab } from 'cc';
import { GameWeapon } from './GameWeapon';
import { Gun } from './Gun';
import PlayerController from '../PlayerController';
import { ResourceUtil } from '../AshFramework/Utils/ResourceUtil';
import { GameManager } from './GameManager';
import { HintController } from './HintController';
import { EventManager, MyEvent } from '../AshFramework/Managers/EventManager';
import { GameData } from './GameData';
import { Bullet } from './Bullet';
import { CAPACITY, UITYPE, WEAPON, WEAPONBULLET } from '../AshFramework/Datas/Constant';
import { ZTool } from '../AshFramework/Utils/ZTool';
import { Magazine } from './Magazine';
import { BundleManager, Bundles } from '../AshFramework/Managers/BundleManager';
import { BulletController } from './BulletController';
import { TLWLSJ_UIManager } from './UIManager';
import { AudioManager, Audios } from '../AshFramework/Managers/AudioManager';
const { ccclass, property } = _decorator;

enum Ani {
    None = "",
    Fire = "kaiqiang",
    Reload = "huandan",
    Load = "lashuan"
}

@ccclass('GameWeapon_LDP')
export class GameWeapon_LDP extends GameWeapon {

    Muzzle: Node = null;
    private _data: Gun = null;
    private _bullets: Bullet[] = [];
    private _capacity: number = 0;
    // IsReload: boolean = false;
    // IsFire: boolean = false;

    protected start(): void {
        this.Muzzle = this.node.getChildByName("枪口");
        this._data = this.Data as Gun;
        PlayerController.Instance.WeaponTs = this;
        this._bullets = GameData.getBulletByWeapon(this.WeaponType);
        this._capacity = CAPACITY[ZTool.GetEnumKeyByValue(WEAPON, this.WeaponType)];
        if (!this._data.CurMagazine) {
            this._data.CurMagazine = new Magazine(this._capacity);
        }
        this.scheduleOnce(() => this.updateWeaponShow(this._data.CurMagazine), 0.1)
    }


    loadBullet() {
        if (TLWLSJ_UIManager.Instance.IsLock) {
            this.getWeapon();
            return;
        }
        if (this.IsFire) return;
        if (this._data.CurMagazine.Bullets.length >= this._capacity) {
            ResourceUtil.LoadPrefab("UI/Hint").then((prefab: Prefab) => {
                const node: Node = instantiate(prefab);
                node.parent = GameManager.Instance.Canvas;
                node.getComponent(HintController).showHint("弹药充足，无需填充弹药！！");
            })
            return;
        } else if (this._bullets.length <= 0) {
            TLWLSJ_UIManager.Instance.showDXPanel(UITYPE.商店, () => {
                TLWLSJ_UIManager.Instance.closeDXPanel();
                this.IsReload = true;
                AudioManager.PlaySound(Audios.Reload);
                this.playAni(Ani.Reload, false, () => {
                    this.State = Ani.None;
                    this.IsReload = false;
                    if (this._data.CurMagazine && this._data.CurMagazine.Bullets.length > 0) {
                        this._data.HaveMagazine.push(this._data.CurMagazine);
                    }
                    const weaponName = ZTool.GetEnumKeyByValue(WEAPON, this.WeaponType);
                    this._data.CurMagazine = new Magazine(CAPACITY[weaponName], WEAPONBULLET[weaponName]);
                    this.updateWeaponShow(this._data.CurMagazine);
                });
            })
            return;
        }
        this.IsReload = true;

        const needNum = this._capacity - this._data.CurMagazine.Bullets.length;
        const curNum = this._bullets[0].Count;
        const fillNum = Math.min(needNum, curNum);
        this.loadCountBullet(fillNum);
    }

    loadCountBullet(count: number) {
        if (count <= 0) {
            this.IsReload = false;
            this.IsStop ? this.endAttack() : this.attack();
            return;
        }
        AudioManager.PlaySound(Audios.Reload);
        this.playAni(Ani.Reload, false, () => {
            this.State = Ani.None;
            this._data.CurMagazine.Bullets.push(this._bullets[0].Name);
            GameData.subBulletByName(this._bullets[0].Name);
            this.updateWeaponShow(this._data.CurMagazine);
            if (this._bullets[0].Count <= 0) this._bullets.shift();
            this.loadCountBullet(count - 1);
        })
    }

    attack() {
        if (TLWLSJ_UIManager.Instance.IsLock) {
            this.getWeapon();
            return;
        }
        if (!this.isAttack()) return;
        if (this.IsStop) {
            this.playAni(Ani.None, false);
            return;
        }
        if (this._data.CurMagazine && this._data.CurMagazine.Bullets.length > 0) {
            this.IsFire = true;
            const bullet = this._data.CurMagazine.Bullets.shift();
            AudioManager.PlaySound(Audios.Fire);
            this.playAni(Ani.Fire, false, () => {
                this.State = Ani.None;
                this.fireBullet(bullet);
                this.updateWeaponShow(this._data.CurMagazine);
                this.playAni(Ani.Load, false, () => {
                    this.IsFire = false;
                    this.attack();
                })
                //发起攻击
            });
        } else {
            this.loadBullet();
        }
    }

    //发射子弹
    fireBullet(bulletName: string) {
        BundleManager.LoadPrefab(Bundles.Prefabs, "子弹_榴弹发射器").then((prefab: Prefab) => {
            const bullet: Node = instantiate(prefab);
            bullet.parent = GameManager.Instance.BulletLayout;
            bullet.setWorldPosition(this.Muzzle.getWorldPosition());
            bullet.getComponent(BulletController).fire(bulletName, PlayerController.Instance.DirX, PlayerController.Instance.DirY);
        });
    }

    protected onEnable(): void {
        super.onEnable();
        EventManager.on(MyEvent.RELOAD, this.loadBullet, this);
    }

    protected onDisable(): void {
        super.onDisable();
        EventManager.off(MyEvent.RELOAD, this.loadBullet, this);
    }

}


